package com.dada.tiku.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.dada.tiku.pojo.DicItem;
import com.dada.tiku.pojo.TUser;
import com.dada.tiku.service.DictionaryService;
import com.dada.tiku.utils.WxResult;

/**
 * 词汇的controller
 * @author Administrator
 *
 */
@RestController
public class DictionaryController {
	
	//由于服务和web分开，web只负责封装数据条件，传递给服务端，调用方法返回数据
	@Reference
	private DictionaryService dictionaryService;
	
	/*
	 * 查看所有的词汇
	 */
	@RequestMapping("/dict/all")
	public String allDict(@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count){
		//分页查询一次不显示所有，异步加载
		WxResult DicItemList = dictionaryService.selectAllDicts(start,count);
		String result = JSON.toJSONString(DicItemList);
		return result;
	}
	
	/*
	 * 根据id查找词汇详情
	 */
	@RequestMapping("/dict/detail")
	public String findDictDetail(Integer id){
		DicItem item = dictionaryService.findDictDetail(id);
		String result = JSON.toJSONString(item);
		return result;
	}
	
	/*
	 * 添加收藏
	 */
	@RequestMapping("/dict/connection")
	public String connectionDic(HttpServletRequest request,Integer id,String openid){
		//收藏该词汇，只有词汇id不够需要用户id，通过过滤器，访问需要登录
		//这里直接取session中的user
		
		WxResult result = dictionaryService.connectionDic(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 取消收藏
	 */
	@RequestMapping("/dict/cancelConn")
	public String cancelConnection(HttpServletRequest request,Integer id,String openid){
		
		WxResult result = dictionaryService.cancelConnection(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 查询词汇是否收藏根据词汇id
	 */
	@RequestMapping("/dict/isCollection")
	public String isCollection(Integer id,String openid){
		WxResult result = dictionaryService.isCollection(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 查询词汇是否为用户添加错题词汇
	 */
	@RequestMapping("/dict/isError")
	public String isError(Integer id,String openid){
		WxResult result = dictionaryService.isError(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 添加错题
	 */
	@RequestMapping("/dic/adderror")
	public String addErrorDic(HttpServletRequest request,Integer id,String openid){
		
		WxResult result = dictionaryService.addErrorDic(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 移除错题
	 */
	@RequestMapping("/dict/removeError")
	public String removeErrorDic(HttpServletRequest request,Integer id,String openid){
		
		WxResult result = dictionaryService.removeErrorDic(id,openid);
		return JSON.toJSONString(result);
	}
	
	/*
	 * 查询收藏词汇列表
	 */
	@RequestMapping("/dict/connList")
	public String connectionList(String openid,@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count){
		WxResult list = dictionaryService.getConnectionList(openid,start,count);
		String result = JSON.toJSONString(list);
		return result;
	}
	/*
	 * 查询错题集合列表
	 */
	@RequestMapping("/dict/errorList")
	public String getErrorList(String openid,@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count){
		WxResult list = dictionaryService.getErrorList(openid,start,count);
		String result = JSON.toJSONString(list);
		return result;
	}
	
	/*
	 * 根据关键字查询词汇
	 */
	@RequestMapping("/dict/query")
	public String queryDict(String keywords,@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count){
		WxResult dicitems = dictionaryService.queryDictByKeywords(keywords,start,count);
		String result = JSON.toJSONString(dicitems);
		return result;
	}
	
	/*
	 * 根据分类查询词汇
	 */
	@RequestMapping("/dict/queryByCategory")
	public String queryDictByCategory(String categoryname,@RequestParam(defaultValue="0")Integer start,@RequestParam(defaultValue="20")Integer count){
		List<DicItem> dicitems = dictionaryService.queryByCategory(categoryname,start,count);
		String result = JSON.toJSONString(dicitems);
		return result;
	}
	
}
